<?php
// 引入工具类
require_once './util/DButil.php';
// 获取请求action参数
$action = $_GET['action']?$_GET['action']:'';


if ($action == 'getlist'){
    // 获取分页参数
    $currentPage = isset($_GET['currentPage'])?$_GET['currentPage']:1;
    $pageSize = isset($_GET['pageSize'])?$_GET['pageSize']:5;
    // 获取查询条件
    $t_type = isset($_GET['t_type'])?$_GET['t_type']:'';
    $recycle_price = isset($_GET['recycle_price'])?$_GET['recycle_price']*100:'';

    // 拼接
    $sqlPara1 = "   ";
    $sqlPara2 = "   ";
    $listArr = array(($currentPage-1)*$pageSize,$pageSize);
    $countArr = array();
    if($t_type != ''){
        $sqlPara1.="  and t_type like ?";
        $t_type = '%'.$t_type.'%';
        array_unshift($listArr,$t_type);
        array_unshift($countArr,$t_type);

    }
    if($recycle_price != ''){
        $sqlPara2.="  and recycle_price=?";
        array_unshift($listArr,$recycle_price);
        array_unshift($countArr,$recycle_price);
    }

    $sql = "select t_id,t_type,recycle_price/100 as recycle_price,create_time,update_time from  treash where 1=1  $sqlPara2  $sqlPara1 limit ?,?";
    $sqlPage = "select count(*) as count from  treash where 1=1  $sqlPara2  $sqlPara1";
    // 查询列表
    $list = selectList($sql,$listArr);
    // 查询总记录数
    $count = selectList($sqlPage,$countArr)[0]['count'];

    // 返回前端的数据封装
    $result = array('page'=>array("currentPage"=>$currentPage,"pageSize"=>$pageSize,"total"=>$count),"list"=>$list);
    echo json_encode($result);
}elseif ($action == 'add'){
    // 获取数据
    $obj = json_decode(file_get_contents("php://input"),true);
    $sql = "insert into treash values(null,?,?,default ,now())";
    echo insAndDelAndUpd($sql,array($obj['t_type'],$obj['recycle_price']*100));

}elseif ($action == 'update'){
// 获取数据
    $obj = json_decode(file_get_contents("php://input"),true);
    $sql = "update treash set t_type=?,recycle_price=?, update_time=now()  where t_id=?";
    echo insAndDelAndUpd($sql,array($obj['t_type'],$obj['recycle_price']*100,$obj['t_id']));
}elseif ($action == 'del'){
    // 获取数据
    $t_id = isset($_GET['t_id'])?$_GET['t_id']:0;
    $sql = "delete from treash where t_id=?";
    echo insAndDelAndUpd($sql,array($t_id));
}elseif ($action == 'getAll'){
    $sql = "select t_id,t_type,recycle_price/100 as recycle_price,create_time,update_time from  treash  ";
    $list = selectList($sql,array());
    echo json_encode($list);
}else{
    echo '不要导弹';
}